package cyou.breathe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cyou.breathe.dto.domain.RouteDTO;
import cyou.breathe.entity.TrainStationDO;
import cyou.breathe.mapper.TrainStationMapper;
import cyou.breathe.service.TrainStationService;
import cyou.breathe.tools.StationCalculateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: breathe
 * @createTime: 2025-06-19
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TrainStationServiceImpl implements TrainStationService {
    private final TrainStationMapper trainStationMapper;
    @Override
    public List<RouteDTO> listTrainStationRoute(String trainId, String startStation, String endStation) {
        LambdaQueryWrapper<TrainStationDO> queryWrapper = Wrappers.lambdaQuery(TrainStationDO.class)
                .eq(TrainStationDO::getTrainId, trainId)
                .select(TrainStationDO::getDeparture);
        List<String> trainStationAllList = trainStationMapper.selectList(queryWrapper)
                .stream()
                .map(TrainStationDO::getDeparture)
                .collect(Collectors.toList());
        return StationCalculateUtils.throughStation(trainStationAllList, startStation, endStation);
    }

    @Override
    public List<RouteDTO> listTakeoutTrainStationRoute(String trainId, String departure, String arrival) {
        LambdaQueryWrapper<TrainStationDO> queryWrapper = Wrappers.lambdaQuery(TrainStationDO.class)
                .eq(TrainStationDO::getTrainId, trainId)
                .select(TrainStationDO::getDeparture);
        List<TrainStationDO> trainStationDOList = trainStationMapper.selectList(queryWrapper);
        List<String> trainStationAllList = trainStationDOList.stream().map(TrainStationDO::getDeparture).collect(Collectors.toList());
        return StationCalculateUtils.takeoutStation(trainStationAllList, departure, arrival);
    }
}
